package com.bamboo.cmsservice.service.impl;

import com.bamboo.cmsservice.entity.CrmBanner;
import com.bamboo.cmsservice.entity.vo.BannerQuery;
import com.bamboo.cmsservice.mapper.CrmBannerMapper;
import com.bamboo.cmsservice.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author jack.wu
 * @since 2020-07-12
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Override
    public Page<CrmBanner> pageBannerCondition(long current, long limit, BannerQuery bannerQuery) {

        //创建page对象
        Page<CrmBanner> pageTeacher = new Page<>(current, limit);
        //构建条件
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        String title = bannerQuery.getTitle();
        String begin = bannerQuery.getBegin();
        String end = bannerQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(title)) {
            //构建条件
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        page(pageTeacher, wrapper);
        return pageTeacher;
    }


    @Override
    @Cacheable(value = "banner", key = "'indexBanner'")
    public List<CrmBanner> selectAllBanner() {
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort", "gmt_create");
        wrapper.last("limit 2");
        return baseMapper.selectList(wrapper);
    }

    @Override
    @CacheEvict(value = "banner", allEntries = true)
    public void addBanner(CrmBanner banner) {
        baseMapper.insert(banner);
    }

    @Override
    @CacheEvict(value = "banner", allEntries = true)
    public void updateBanner(CrmBanner banner) {
        baseMapper.updateById(banner);
    }

    @Override
    @CacheEvict(value = "banner", allEntries = true)
    public void removeBanner(String id) {
        baseMapper.deleteById(id);
    }

}
